﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRUD_ADO.Net.DAO;
using CRUD_ADO.Net.BUS;
using System.Data;


namespace CRUD_ADO.Net.GUI
{
    public partial class DemoADO_NET : System.Web.UI.Page
    {
        private BUSdemo bus;
        System.Text.Encoding en1 = System.Text.Encoding.Unicode;
        System.Text.Encoding en2 = System.Text.Encoding.UTF8;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex != -1)
            {
                GridViewRow row = GridView1.SelectedRow;
                string CustomerID = row.Cells[1].Text.ToString();
                string CompanyName =  HttpUtility.HtmlDecode(row.Cells[2].Text.ToString());
                string ContactName = HttpUtility.HtmlDecode(row.Cells[3].Text.ToString());   
                string Address =  HttpUtility.HtmlDecode(row.Cells[4].Text.ToString());
                string Phone = row.Cells[5].Text.ToString();
                
                txtCustomerID.Text = CustomerID;
                txtCommanyName.Text =CompanyName;
                txtContacName.Text = ContactName;
                txtAddress.Text =Address;
                txtPhone.Text = Phone;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            if (btnAddnew.Text == "Add new")
            {
                btnAddnew.Text = "Insert";
                setnullTextbox();
                txtCustomerID.Focus();
                GridView1.Enabled = false;
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btnRefesh.Enabled = false;

            }
            else if (btnAddnew.Text == "Insert")
            {
                bus = new BUSdemo();
                string CustomerId = txtCustomerID.Text.Trim();
                string CompanyName = txtCommanyName.Text.Trim();
                string ContactName = txtContacName.Text.Trim();
                string Address = txtAddress.Text.Trim();
                string Phone = txtPhone.Text.Trim();
                if (CustomerId != null && CompanyName != null && ContactName != null && Address != null && Phone != null)
                {
                    if (CustomerId.Length <= 5)
                    {
                        bool status = bus.insertCustomer(CustomerId, CompanyName, ContactName, Address, Phone);
                        setnullTextbox();

                        if (status == true)
                        {
                            lblStatus.Text = "Insert Success..!";
                            lblStatus.ForeColor = System.Drawing.Color.Green;
                            btnAddnew.Text = "Add new";

                            GridView1.Enabled = true;
                            btnUpdate.Enabled = true;
                            btnDelete.Enabled = true;
                            btnRefesh.Enabled = true;
                        }
                        else
                        {
                            lblStatus.Text = "Insert False  .. !";
                        }
                    }
                    else
                        lblStatus.Text = "Customer ID Duoc nha 5 ky tu tro xuong";
                }
                else
                {
                    setnullTextbox();
                    txtCustomerID.Focus();
                    lblStatus.Text = "Yêu Cầu điền đầy đủ thông tin ";
                }
            }
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            bus = new BUSdemo();

            if (GridView1.SelectedIndex != -1)
            {
                string CustomerId = txtCustomerID.Text.Trim();
                bool test = bus.deleteCustomer(CustomerId);
                if (test == true)
                {
                    lblStatus.Text = "Delete Success..!";
                    lblStatus.ForeColor = System.Drawing.Color.Green;
                }
                else
                    lblStatus.Text = "Delete False có thể do bị ràng buộc khóa ngoại..";
            }
            else
                lblStatus.Text = "Mời bạn chon Customer cần xóa ";
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            bus = new BUSdemo();
            if (btnUpdate.Text == "Update")
            {
                if (GridView1.SelectedIndex == -1)
                {
                    lblStatus.Text = "Moi ban chon Customer can update";
                }
                else
                {
                    btnUpdate.Text = "Insert";
                    btnAddnew.Enabled = false;
                    btnDelete.Enabled = false;
                    btnRefesh.Enabled = false;
                    GridView1.Enabled = false;
                    txtCustomerID.Enabled = false;
                }

            }
            else if (btnUpdate.Text == "Insert")
            {
                string CustomerId = txtCustomerID.Text.Trim();
                string CompanyName = txtCommanyName.Text.Trim();
                string ContactName = txtContacName.Text.Trim();
                string Address = txtAddress.Text.Trim();
                string Phone = txtPhone.Text.Trim();
                if (CustomerId != null && CompanyName != null && ContactName != null && Address != null && Phone != null)
                {
                    bool status = bus.updateCustomer(CustomerId, CompanyName, ContactName, Address, Phone);
                    if (status == true)
                    {
                        lblStatus.Text = "Update Success..!!";
                        lblStatus.ForeColor = System.Drawing.Color.Green;
                        btnAddnew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnRefesh.Enabled = true;
                        GridView1.Enabled = true;
                        txtCustomerID.Enabled = true;
                        btnUpdate.Text = "Update";
                    }
                    else
                    {
                        lblStatus.Text = "Update False";
                    }

                }
                else
                {
                    setnullTextbox();
                    txtCustomerID.Focus();
                    lblStatus.Text = "Yêu Cầu điền đầy đủ thông tin ";
                }
            }
        }

        protected void btnRefesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }


        public void LoadData()
        {
            DAODemo ad = new DAODemo();
            DataTable table = ad.SelectData();
            GridView1.DataSource = table;
            GridView1.DataBind();
            txtCountItem.Text = "Total Items: " + ad.CountItem();
        }
        public void setnullTextbox()
        {
            txtAddress.Text = "";
            txtCommanyName.Text = "";
            txtContacName.Text = "";
            txtCustomerID.Text = "";
            txtPhone.Text = "";

        }
        private string Encode(string text)
        {
            byte[] encodedText = System.Text.Encoding.UTF8.GetBytes(text);
            return System.Convert.ToBase64String(encodedText);
        }
        private string Decode(string encodedText)
        {
            byte[] decodedText = System.Convert.FromBase64String(encodedText);
            return System.Text.Encoding.UTF8.GetString(decodedText);
        }
    }
}